package org.openintents.droidcon.cloud.model;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.openintents.droidcon.cloud.PMF;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class Room {

	@NotPersistent
	private static PersistenceManager pm;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key id;

	@Persistent
	private String name;
	@Persistent
	private String alias;

	public Room(String name, String alias) {
		this.name = name;
		this.alias = alias;
	}

	public Key getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	/**
	 * Returns the PersistenceManager for the Schedule Class, creates one if not
	 * existent or closed
	 * 
	 * @return PersistenceManager
	 */
	private static PersistenceManager getPersistenceManager() {
		if (pm == null) {
			pm = PMF.get().getPersistenceManager();
		} else if (pm.isClosed()) {
			pm = PMF.get().getPersistenceManager();
		}
		return pm;
	}
	/**
	 * Deletes the Schedule object from the persistent storage
	 * 
	 * @return
	 */
	public boolean delete() {
		boolean wasSuccessful;
		try {
			getPersistenceManager().deletePersistent(this);
			wasSuccessful = true;
		} catch (Exception e) {
			wasSuccessful = false;
		} finally {
			getPersistenceManager().close();
		}
		return wasSuccessful;
	}

}
